//
//  TurnViewController.m
//  Nurse
//
//  Created by Sunny on 13-12-13.
//  Copyright (c) 2013年 RainSets. All rights reserved.
//

#import "TurnViewController.h"
#import "InputHospitalViewController.h"
#import "ScanService.h"
#import "PatientService.h"
#import "PersonalTurnViewController.h"

@interface TurnViewController ()
@property(nonatomic,strong)NSString* scanzyh;
-(void)onReceiveScanData:(NSNotification*)notification;
-(void)pushPatientView:(NSDictionary*) record;
-(void)updatePID:(NSString*)pid;
@end

@implementation TurnViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
        [nc addObserver:self selector:@selector(onReceiveScanData:) name:@"TurnView" object:nil];
    }
    return self;
}


-(void)onReceiveScanData:(NSNotification*)notification{
    //-(void)decoderDataReceived:(NSString *)smdata{
    
    NSDictionary* userinfo = [notification userInfo];
    NSString* data = [userinfo valueForKey:@"data"];
    //[[DataEngine sharedInstance]showMessage:data withTitle:@"AAAAA"];
    if ([data length] ==9) {
        NSArray* brarray = [data componentsSeparatedByString:@"|"];
        if ([brarray count] ==2) {
            //[[DataEngine sharedInstance]showMessage:@"没有找到此条码对应的病人" withTitle:@"扫描错误2"];
            //[[DataEngine sharedInstance]showMessage:@"A2" withTitle:@"AAAAA"];
            NSString* zyh = [brarray objectAtIndex:1];
            
            [self updatePID:zyh];
        }
        else{
            [[DataEngine sharedInstance]showMessage:@"错误的病人条码格式" withTitle:@"扫描错误"];
        }
    }
}

-(void)updatePID:(NSString*)pid{
    __weak TurnViewController* this = self;
    //[[DataEngine sharedInstance]showMessage:@"A3" withTitle:@"AAAAA"];
    [[PatientService sharedInstance]queryPatientsForPID:pid withSuccessHandler:^(id result) {
        if ([result isKindOfClass:[NSArray class]]) {
            if ([result count]>0) {
                //[[DataEngine sharedInstance]showMessage:@"AAAAAA" withTitle:@"AAAAA"];
                NSDictionary* record = [result objectAtIndex:0];
                [this pushPatientView:record];
            }else{
                [[DataEngine sharedInstance]showMessage:@"没有找到此条码对应的病人" withTitle:@"扫描错误"];
            }
        }
        else{
            [[DataEngine sharedInstance]showMessage:@"返回数据类型错误期望为NSArrray" withTitle:@"扫描错误"];
        }
        
    } andFailHandler:^(NSError *error, NSString *title) {
        [[DataEngine sharedInstance]showMessage:@"调用Web服务错误" withTitle:@"扫描错误"];
    }];
}


-(void)pushPatientView:(NSDictionary *)record{
    [[PatientService sharedInstance]setCurrentPatient:record];
    PersonalTurnViewController *vc = [[PersonalTurnViewController alloc]init];
    self.navigationController.hidesBottomBarWhenPushed = YES;
    vc.hidesBottomBarWhenPushed = YES;
    [App hideTabbar];
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [App showTabbar];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    self.title  = @"流转";
    
    
    self.bottomImg.image=[UIImage imageNamed:@"patient-icon-l.png"];
    self.scanningimg.image=[UIImage imageNamed:@"scan-icon-s.png"];
    self.describeLab.text=@"请扫描病人条形码";
    
    //给图层添加一个有色边框
    self.inputboxImg.layer.borderWidth = 2;
    self.inputboxImg.layer.borderColor = [NavigationBarColor CGColor];
    //self.view.backgroundColor
   // [self.inputboxBtn setTitle:@"输入住院号" forState:UIControlStateNormal];
  
    [self.inputboxBtn setTitleColor:NavigationBarColor forState:UIControlStateNormal];
    [self setNav];
    
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,
                          [UIFont boldSystemFontOfSize:19],UITextAttributeFont,
                          nil];
    
    self.navigationController.navigationBar.titleTextAttributes = dict;
    
    //[self updatePID:@"510452"];
    
}
-(void)setNav{
    //[TSMessage setDefaultViewController:[Router shared].navigationController];
    
    if([UINavigationBar instancesRespondToSelector:@selector(setShadowImage:)])
    {
        [[UINavigationBar appearance] setShadowImage:[UIImage new]];
    }
    
    UINavigationBar *navBar = self.navigationController.navigationBar;
    
    if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
    {
        //if iOS 5.0 and later
        
        [navBar setBackgroundImage:[UIImage imageNamed:@"head-bg.png"] forBarMetrics:UIBarMetricsDefault];
    }
    
}


-(IBAction)inputboxBtn_click:(id)sender{
    
    InputHospitalViewController *vc = [[InputHospitalViewController alloc]init];
    self.navigationController.hidesBottomBarWhenPushed = YES;
    vc.hidesBottomBarWhenPushed = YES;
    [App hideTabbar];
    [self.navigationController pushViewController:vc animated:YES];
    
    
}
-(void)decoderDataReceived:(NSString *)data{
    //    NSLog(@"Decoder Data Received: %@",data);
    UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:nil message:data delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alertV show];
    //self.decoderData.text = data;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//-(IBAction)scanclick:(id)sender{
//     [[Captuvo sharedCaptuvoDevice]startDecoderScanning];
//}
//
//
//- (IBAction)scanOffButton:(id)sender {
//    [[Captuvo sharedCaptuvoDevice]stopDecoderScanning];
//}


@end
